<template>
  <el-form :model="profile" ref="form" v-loading="loading" label-position="top">
    <Upload imageClass="w-28 h-28 rounded-full mx-auto" v-model="profile.avatar" />
    <el-form-item
      label="用户名"
      prop="username"
      class="mt-2"
      :rules="[
        {
          required: true,
          message: '用户名必须填写'
        }
      ]"
    >
      <el-input v-model="profile.username" placeholder="请填写用户名" />
    </el-form-item>
    <el-form-item
      label="账号"
      prop="mobile"
      :rules="mobileRules"
    >
      <el-input v-model="profile.mobile" placeholder="请填写账号"  show-word-limit maxlength="20"/>
    </el-form-item>
    
    <el-form-item label="密码" prop="password" :rules="passwordRules">
      <el-input v-model="profile.password" type="password" show-password placeholder="请输入密码" />
    </el-form-item>
    <div class="flex justify-end">
      <el-button type="primary" @click="submitForm(form)">{{ $t('system.update') }}</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
// @ts-nocheck
import { onMounted, ref } from 'vue'
import { useCreate } from '@/composables/curd/useCreate'
import http from '@/support/http'
import { useUserStore } from '@/stores/modules/user'

// 定义emit事件
const emit = defineEmits(['success'])

interface profileInterface {
  avatar: string
  username: string
  mobile: string
  password: string
}

const profile = ref<profileInterface>(
  Object.assign({
    avatar: '',
    username: '',
    mobile: '',
    password: ''
  })
)
const { form, loading, submitForm, afterCreate } = useCreate('user/online', null, profile)

const passwordRules = [
  {
    required: false,
    message: '密码必须填写'
  },
  {
    pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/,
    message: '必须包含大小写字母和数字的组合，可以使用特殊字符，长度在6-20之间'
  }
]

const mobileRules = [
  {
    required: true,
    message: '登录账号必须填写'
  },
  {
    pattern: /^[0-9a-z]{6,20}$/,
    message: '账号只能包含数字和小写字母，长度为6-20位'
  }
]

const getUserInfo = () => {
  loading.value = true
  http.get('user/online').then(r => {
    profile.value.username = r.data.data.username
    profile.value.avatar = r.data.data.avatar
    profile.value.mobile = r.data.data.mobile
    loading.value = false
  })
}

onMounted(() => {
  getUserInfo()
})

const userStore = useUserStore()

afterCreate.value = () => {
  userStore.getUserInfo()
  // 触发成功事件，通知父组件
  emit('success')
}
</script>

<style scoped>
:deep(.el-upload) {
  @apply h-full w-full;
}
</style>
